package com.cenwangkeji.resident.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.AssessBean;
import com.cenwangkeji.resident.model.bean.SignBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.et)
    EditText mEt;

    @Override
    public int setLayoutId() {
        return R.layout.activity_feedback;
    }




    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        final String content = mEt.getText().toString();
        if (TextUtils.isEmpty(content)){
            MyToast.show("内容不能为空");
        }else {
            showLoading();
            Observable.create(new ObservableOnSubscribe<SuccessBean>() {
                @Override
                public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                    Map<Object,Object> map = new HashMap<>();
                    map.put( Constant.token, User.getToken());
                    map.put( "mark",3);
                    map.put("fkxx",content);
                    JSONObject jsonObject = new JSONObject(map);
                    MyLogger.i(jsonObject.toString());
                    String json = OkHttpUtils.post(jsonObject.toString(), Constant.feedback);
                    MyLogger.i(json);
                    Gson gson = new Gson();
                    emitter.onNext(gson.fromJson(json, SuccessBean.class ));
                }
            }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
                @Override
                public void onNext(SuccessBean bean) {
                    if (bean.getMsg().equals(Constant.token_expired)){
                        signIn();
                    }else {
                        if (bean.getCode() ==0){
                            MyToast.show("提交成功");
                        }else {
                            MyToast.show(bean.getMsg());
                        }
                    }
                    dismissLoading();
                    finish();

                }
            });
        }
    }
}
